package array

func rob2(nums []int) int {

	if len(nums) == 1 {
		return nums[0]
	}

	max := func(i, j int) int {
		if i > j {
			return i
		}
		return j
	}

	helper := func(nums []int) int {
		dp := make([]int, len(nums)+1)
		dp[0] = 0
		dp[1] = nums[0]

		for i := 2; i < len(dp); i++ {
			dp[i] = max(dp[i-1], dp[i-2]+nums[i-1])
		}
		return dp[len(dp)-1]
	}

	return max(helper(nums[0:len(nums)-1]), helper(nums[1:]))

}
